package com.jobchecks.common.baseresult;

import com.jobchecks.common.enums.RespCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import java.io.Serializable;

/**
 * 接口基础响应体
 *
 * @author huangjinjia
 */
@Data
@ApiModel(value="接口响应体", description="接口响应体")
public class JobchecksResult<T> implements Serializable{
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "响应编码")
    private Integer respCode;

    @ApiModelProperty(value = "响应信息")
    private String respMsg;

    @ApiModelProperty(value = "响应数据")
    private T data;

    /**
     * 返回错误消息
     */
    public static JobchecksResult error() {
        return JobchecksResult.newInstance(RespCodeEnum.FAIL.getCode(), RespCodeEnum.FAIL.getDesc(), null);
    }

    /**
     * 返回错误消息
     *
     * @param respMsg 错误消息
     */
    public static JobchecksResult error(String respMsg) {
        return JobchecksResult.newInstance(RespCodeEnum.FAIL.getCode(), respMsg, null);
    }

    /**
     * 返回错误消息
     *
     * @param respCode 错误编码
     * @param respMsg  错误消息
     */
    public static JobchecksResult error(Integer respCode, String respMsg) {
        return JobchecksResult.newInstance(respCode, respMsg, null);
    }

    /**
     * 响应成功数据
     */
    public static JobchecksResult success() {
        return JobchecksResult.newInstance(RespCodeEnum.SUCCESS.getCode(), RespCodeEnum.SUCCESS.getDesc(), null);
    }

    /**
     * 响应成功数据
     *
     * @param data 响应数据
     */
    public static JobchecksResult success(Object data) {
        return JobchecksResult.newInstance(RespCodeEnum.SUCCESS.getCode(), RespCodeEnum.SUCCESS.getDesc(),  data);
    }

    /**
     * 响应结果转化
     *
     * @param respCode 响应码
     * @param respMsg  响应信息
     * @param data     响应数据
     */
    private static <T> JobchecksResult<T> newInstance(Integer respCode, String respMsg, T data) {
        JobchecksResult<T> jobchecksResult = new JobchecksResult<>();
        jobchecksResult.setRespCode(respCode);
        jobchecksResult.setRespMsg(respMsg);
        jobchecksResult.setData(data);
        return jobchecksResult;
    }
}
